package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.GoodsSalesVO;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    @Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,\n" +
            " phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number,\n" +
            " tableware_status)\n" +
            " values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            " #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            " #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Orders orders);

    void update(Orders orders);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    /**
     * 根据状态统计订单数量
     *
     * @param status
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer countStatus(Integer status);

    @Select("select * from orders where status=#{status} and order_time <=#{before}")
    List<Orders> selectByStatuAndOrderTime(Integer status, LocalDateTime before);

    @Select("select sum(amount) from orders where status=5 and order_time between #{begin} and #{end}")
    Double turnover(LocalDateTime begin, LocalDateTime end);


    Integer countStatusOrder(Map map);

    @Select("select od.name,sum(od.number) number " +
            " from orders o ,order_detail od " +
            " where o.id=od.order_id " +
            " and o.status=5 " +
            " and o.order_time between  #{beginTime} and #{endTime}" +
            " group by od.name " +
            " order by number desc " +
            " limit 10")
    List<GoodsSalesVO> countTop10Goods(LocalDateTime beginTime, LocalDateTime endTime);
}
